<?php
	
	/**
	 * PkRouter File.
	 * 
	 * @author Brian Bal <brian@youbeqb.com>
	 * @version 1.0
	 * @package pink-koala
	 */
	
	/**
	 * PkRouter class
	 *
	 * @package pink-koala
	 * @author Brian Bal
	 **/
	
	class PkRouter
	{
 		/**
 		 * The route function will take the route as a parameter and return the controller and action names
		 * as a string in an array.
 		 *
 		 * @return array
 		 * @author Brian Bal
 		 **/
		function route($route, $routes)
		{
			$path_components = $this->filterRoute($route);
			
			$matched_route = $this->matchRoutes($path_components, $routes);
			
			//default controller to be called 'welcome'
			if(! isset($matched_route['controller']) )
			{
				$matched_route['controller'] = "welcome";
			}
			
			// default action to be called 'index'
			if(! isset($matched_route['action']) )
			{
				$matched_route['action'] = 'index';
			}
			
			/*
			echo "<h3>path_components</h3><pre>\n";
			print_r($path_components);
			echo "</pre>";
			
			echo "<h3>matched_route</h3><pre>\n";
			print_r($matched_route);
			echo "</pre>";
			*/
			
			return $matched_route;
		}
		
 		function filterRoute($route)
 		{
			// break apart the query string from the path
			$parts = explode('?', $route);
			
			$path = "";
			if( count($parts) > 0 )
			{
				$path = $parts[0];
			}
			
			$query_string = "";
			if( count($parts) > 1 )
			{
				$query_string = $parts[1];
			}
			
			
			// get rid of the root path
			$path = ereg_replace( "^" . WWW_DIR, "", $path);
			
			// get rid of trailing /
			$path = ereg_replace("/$", "", $path);
			
			// get rid of first /
			$path = ereg_replace("^/", "", $path);
			
			// break up the path
			$path_components = explode('/', $path);
			
			return $path_components;
		}
		
		function matchRoutes($path_components, $routes)
		{
			$matched_route = array("controller" => "welcome", "action" => "index");
			
			// try to match the path to the routes
			$not_found = true;
			foreach($routes as $route)
			{
				// break the route into an array
				$route_components = explode('/', $route['url']);
				
				// see if the path_components match the route
				if( (count($path_components) == count($route_components)) && $not_found)
				{
					// see if these two routes match
					$match = true;
					for($i = 0; $i < count($path_components); $i ++)
					{
						$pth_part = $path_components[$i];
						
						$rot_part = $route_components[$i];
						$rot_part = ereg_replace("^\:(.*)", $pth_part, $rot_part);
						
						if($pth_part != $rot_part)
						{
							$match = false;
						}
					}
					
					if($match)
					{
						$count = 0;
						
						/*
						echo "route <pre>\n";
						print_r($route);
						echo "</pre>\n";
						
						echo "path_components<pre>\n";
						print_r($path_components);
						echo "</pre>\n";
						*/
						
						foreach($route_components as $key => $value)
						{
							$value = str_replace( ":", "", $value);
							
							//echo "<p>value: $value, path_components[$count]: {$path_components[$count]}</p>";
							
							if($value != "controller" && $value != "action")
							{
								$_GET[$value] = $path_components[$count];
							}else{
								$matched_route[$value] = $path_components[$count];
							}
							
							$count ++;
						}
						
						foreach($route as $key => $val)
						{
							if($key != "url")
							{
								$matched_route[$key] = $val;
							}
						}
						
						$not_found = false;
						return $matched_route;
					}
				}
				
			}
		}
		
	}